package com.intelligence.pda.ui.modifypwd;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.intelligence.androidlibrary.utils.SharePreferenceUtils;
import com.intelligence.pda.R;
import com.intelligence.pda.parameter.ResetPasswordParameter;
import com.intelligence.pda.retrofit.ApiCallback;
import com.intelligence.pda.ui.BaseActivity;
import com.intelligence.pda.ui.login.LoginActivity;
import com.intelligence.pda.ui.signout.model.LoginOutModel;
import com.intelligence.pda.utils.AppConstants;

import butterknife.Bind;

public class ModifyPwdActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.ll_toolbar_return)
    LinearLayout ll_toolbar_return;
    @Bind(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @Bind(R.id.tv_determine)
    TextView tv_determine;
    @Bind(R.id.et_pwd_used)//原密码
            EditText et_pwd_used;
    @Bind(R.id.et_pwd_new)//新密码
            EditText et_pwd_new;
    @Bind(R.id.et_pwd_modify)//确认密码
            EditText et_pwd_modify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        tv_toolbar_title.setText("修改登录密码");
        listener();
    }

    private void listener() {
        ll_toolbar_return.setOnClickListener(this);
        tv_determine.setOnClickListener(this);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_toolbar_return://返回
                finish();
                break;
            case R.id.tv_determine://确定修改
                if (getView()) {
                    getUserInfo();
                }
                break;

        }
    }

    private boolean getView() {
        if (TextUtils.isEmpty(et_pwd_used.getText().toString())) {
            toastShow("原密码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(et_pwd_new.getText().toString())) {
            toastShow("新密码不能为空");
            return false;
        }
        if (TextUtils.isEmpty(et_pwd_modify.getText().toString())) {
            toastShow("确认密码不能为空");
            return false;
        }

        if (et_pwd_used.getText().length() < 5) {
            toastShow("原密码6~18之间");
            return false;
        }
        if (et_pwd_new.getText().length() < 5) {
            toastShow("新密码6~18之间");
            return false;
        }
        if (et_pwd_modify.getText().length() < 5) {
            toastShow("确认密码6~18之间");
            return false;
        }
        if (!et_pwd_new.getText().toString().equals(et_pwd_modify.getText().toString())) {
            toastShow("两次密码不一致");
            return false;
        }
        return true;
    }

    private void getUserInfo() {
        //修改密码
        ResetPasswordParameter resetPasswordParameter = new ResetPasswordParameter();
        resetPasswordParameter.setBasicStr(SharePreferenceUtils.getString(AppConstants.BASIC_STR));
        resetPasswordParameter.setOldPassword(et_pwd_used.getText().toString());
        resetPasswordParameter.setNewPassword(et_pwd_new.getText().toString());
        addSubscription(apiStores.getResetPassword(resetPasswordParameter),
                new ApiCallback<LoginOutModel>() {
                    @Override
                    public void onSuccess(LoginOutModel model) {
                        startActivity(new Intent(mActivity, LoginActivity.class));
                        SharePreferenceUtils.putString(AppConstants.EDIT_NAME, "");
                        SharePreferenceUtils.putString(AppConstants.EDIT_PWD, "");
                        activityManagerUtil.finishAllActivity();
                    }

                    @Override
                    public void onFailure(LoginOutModel model, String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }


                    @Override
                    public void onFinish() {
                    }

                });
    }


}
